package com.sso.service;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import com.yyl.common.pojo.TaotaoResult;
import com.yyl.mapper.TbUserMapper;
import com.yyl.pojo.TbUser;
import com.yyl.pojo.TbUserExample;
import com.yyl.pojo.TbUserExample.Criteria;
/**
 * 注册服务
 * @author yangyl
 *
 */
@Service
public class RegisterServiceImpl implements RegisterService {
	
	@Autowired
	private TbUserMapper userMapper;
	
	@Override
	public TaotaoResult checkData(String param, int type) {
		// 根据数据类型检查数据
		TbUserExample example = new TbUserExample();
		Criteria criteria = example.createCriteria();
		if(1 == type) {
			criteria.andUsernameEqualTo(param);
		} else if(2 == type) {
			criteria.andPhoneEqualTo(param);
		} else if(3 == type) {
			criteria.andEmailEqualTo(param);
		}
		//执行查询
		List<TbUser> list = userMapper.selectByExample(example);
		//判断查询结果是否为空
		if(list == null || list.isEmpty()) {
			return TaotaoResult.ok(true);
		} else {
			return TaotaoResult.ok(false);
		}
	}

	@Override
	public TaotaoResult register(TbUser user) {
		TaotaoResult result = null;
		// 校验数据
		//校验用户名和密码不能为空
		if(StringUtils.isBlank(user.getUsername()) || StringUtils.isBlank(user.getPassword())) {
			return TaotaoResult.build(400, "用户名或密码不能为空");
		} 
		//校验是否重复
		//校验用户名
		result = checkData(user.getUsername(),1);
		if(!(boolean)result.getData()) {
			return TaotaoResult.build(400, "用户名已存在");
		}
		//校验手机号
		if(user.getPhone() != null) {
			result = checkData(user.getPhone(),2);
			if(!(boolean)result.getData()) {
				return TaotaoResult.build(400, "手机号已存在");
			}
		}
		//校验邮箱
		if(user.getEmail() != null) {
			result = checkData(user.getEmail(),3);
			if(!(boolean)result.getData()) {
				return TaotaoResult.build(400, "邮箱已存在");
			}
		}
		//补全数据
		user.setCreated(new Date());
		user.setUpdated(new Date());
		//对密码进行md5加密
		user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
		userMapper.insert(user);
		return TaotaoResult.ok();
	}

}
